package com.lxd.kyvideo.controller.backend;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lxd.kyvideo.common.ApiResult;
import com.lxd.kyvideo.pojo.Carousel;
import com.lxd.kyvideo.service.CarouselService;
import com.lxd.kyvideo.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
@RequestMapping("/api/manager/carousel")
@Api(tags = "轮播管理")
public class BCarouselController {

    @Resource
    private CarouselService carouselService;


    @ApiOperation("按标题分页查询轮播列表")
    @GetMapping("/queryPage")
    /*
paramType:
 1、@RequestParam     paramType="query"
 2、@PathVariable     paramType="path"
 3、 @RequestBody     paramType="body/form"
     */
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "imgTitle",value = "标题",required = false,paramType = "query",dataType = "String",defaultValue = ""),
                    @ApiImplicitParam(name = "page",value = "页码",required = true,paramType = "query",dataType = "Integer",defaultValue = "1"),
                    @ApiImplicitParam(name = "limit",value = "页容量",required = true,paramType = "query",dataType = "Integer",defaultValue = "10")
            }
    )
    public ApiResult queryPage(
            @RequestParam(required = false,defaultValue = "") String imgTitle,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit){
        IPage<Carousel> iPage = carouselService.findCarouselsByPage(imgTitle,page,limit);
        PageVO<Carousel> pageVO = new PageVO<>();
        pageVO.setTotal(iPage.getTotal());
        pageVO.setList(iPage.getRecords());
        return ApiResult.ok(pageVO);
    }


    @ApiOperation("新增轮播图")
    @PostMapping("/addCarousel")
    public ApiResult addCarousel(@RequestBody Carousel carousel){
        boolean retVal = carouselService.save(carousel);
        if(retVal) {
            return ApiResult.ok(null,"保存轮播图成功！");
        }
        return ApiResult.fail("保存轮播图失败！");
    }


    @ApiOperation("删除轮播图")
    @DeleteMapping("/deleteCarousel/{id}")
    public ApiResult deleteCarousel(@PathVariable Integer id){
        boolean retVal = carouselService.removeById(id);
        if(retVal ) {
            return ApiResult.ok(null,"删除轮播图成功！");
        }
        return ApiResult.fail("删除轮播图失败！");
    }


    @ApiOperation("修改轮播图")
    @PutMapping("/updateCarousel")
    public ApiResult updateCarousel(@RequestBody Carousel carousel){
        boolean retVal = carouselService.updateById(carousel);
        if(retVal ) {
            return ApiResult.ok(null,"修改轮播图成功！");
        }
        return ApiResult.fail("修改轮播图失败！");
    }


    @ApiOperation("获取轮播图详情")
    @GetMapping("/query/{id}")
    public ApiResult queryById(@PathVariable Integer id){
        Carousel carousel = carouselService.getById(id);
        if(carousel != null ) {
            return ApiResult.ok(carousel,"OK");
        }
        return ApiResult.fail("该轮播编号无效！");
    }
}
